package com.onlyphoto.onlymall.mvp.view.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.app.ActivityCompat
import androidx.core.app.SharedElementCallback
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.response.GoodsDetailsBean
import com.onlyphoto.onlymall.mvp.contract.CGoodsDetails
import com.onlyphoto.onlymall.mvp.presenter.PGoodsDetailsImpl
import q.rorbin.badgeview.QBadgeView
import java.util.ArrayList
import kotlin.math.min




class ProductDetailActivity2 : BaseActivity<PGoodsDetailsImpl>(), CGoodsDetails.IVGoodsDetails {

    override fun showEmpty() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun add2CartSuccess() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getDetailsSuccess(bean: GoodsDetailsBean) {

    }

    private val images = arrayOf("http://iph.href.lu/750x360?text=%E8%BD%AE%E6%92%AD%E5%9B%BE1&fg=FF5252&bg=E4FAFF", "http://iph.href.lu/750x360?text=%E8%BD%AE%E6%92%AD%E5%9B%BE2&fg=ffffff&bg=ff6e00")
    private var mReenterState: Bundle? = null
    var parent: ViewGroup? = null
    var badgeView: QBadgeView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val pictureList = ArrayList<String>()
        pictureList.addAll(images)

        parent = findViewById<View>(R.id.container) as ViewGroup
        val cnt = min(images.size, parent!!.childCount)
        for (i in 0 until cnt) {
            val imageView = parent!!.getChildAt(i) as ImageView
            Glide.with(this).load(images[i]).into(imageView)
            imageView.setOnClickListener {
                PhotoBrowseActivity().startWithElement(this, pictureList, i, imageView)
            }

        }

        setSharedElementCallback(this)
    }

    /**
     * 接管Activity的setExitSharedElementCallback
     * @param activity
     */
    private fun setSharedElementCallback(activity: Activity) {
        ActivityCompat.setExitSharedElementCallback(activity, object : SharedElementCallback() {
            override fun onMapSharedElements(names: List<String>, sharedElements: MutableMap<String, View>) {
                if (mReenterState != null) {
                    val index = mReenterState!!.getInt("index", 0)
                    sharedElements.clear()
                    sharedElements["tansition_view"] = parent!!.getChildAt(index)
                    mReenterState = null
                }
            }
        })

    }

    override fun onActivityReenter(resultCode: Int, data: Intent) {
        super.onActivityReenter(resultCode, data)
        mReenterState = Bundle(data.extras)
    }

    override fun retry() {}

    override fun loadData() {
        //加载数据
    }

    override fun init() {
        badgeView = QBadgeView(this).bindTarget(findViewById(R.id.img)).setBadgeNumber(1).setGravityOffset(0f, true).setBadgeTextSize(10f, true).setBadgePadding(3f, true).setOnDragStateChangedListener { dragState, badge, targetView -> } as QBadgeView?
    }

    override fun initListener() {
        //点击事件初始化
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {

    }

    override fun setContentViewId(): Int {
        return R.layout.activity_productdetail2
    }

    override fun createPresenter() {
        mPresenter = PGoodsDetailsImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

}
